package com.luke.base.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.luke.base.domain.RetrieveAlarm;
import com.luke.base.domain.vo.Location;
import com.luke.base.vo.Status;
import com.luke.common.core.domain.AjaxResult;
import java.util.ArrayList;
import java.util.List;

public class LukeApiUtil {
    public static final String URL="Http://position.gnets.cn:8090/gpstar/";
    public static final String CUSTID="PRY9I2xU";

    //1.获取最后一次定位位置
    public static AjaxResult location(String carId) throws HttpStatusException {
        String url=URL+"location?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            JSONArray jsonArray= (JSONArray) obj.get("location");
            Location location= JSON.toJavaObject((JSON) jsonArray.get(0),Location.class);
            return AjaxResult.success(location);
        }
        return AjaxResult.error(obj.get("code").toString());
    }
    //2.获取轨迹信息
    public static AjaxResult track(String carId,String startTime ,String endTime) throws HttpStatusException {
        String url=URL+"track?cust_id="+CUSTID+"&car_id="+carId+"&start_time="+startTime+"&end_time="+endTime;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        //System.err.println((Integer)obj.get("code")!=20013);
        List<Location> locationList=new ArrayList<>();
        if(("OK").equals(obj.get("ret"))){
            locationList=JSONArray.parseArray(obj.get("track").toString(),Location.class);
        }else {
            Integer code=(Integer)obj.get("code");
            if(code!=null && code != 20013){
                return AjaxResult.error(code+"");
            }
        }
        return AjaxResult.success(locationList);
    }
    //3.查询指定范围内的设备
    public static String around(String maxLon,String minLon ,String maxLat ,String minLat) throws HttpStatusException {
        String url=URL+"around?cust_id="+CUSTID+"&maxlon=117012345&minlon=117012245&maxlat=36012345&minlat=36012344";
        return HttpUtil.doGet(url,"GET");
    }

    //4.查询设备状态
    public static AjaxResult status(String carId) throws HttpStatusException {
        String url=URL+"status?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            Status status= JSON.toJavaObject((JSONObject) obj.get("status"),Status.class);
            return AjaxResult.success(status);
        }
        return AjaxResult.error(obj.get("code").toString());
    }

    public static JSONObject LocationSummary() throws HttpStatusException{
        String url=URL+"LocationSummary?cust_id="+CUSTID;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        return obj;
    }

    public static List<Location> LocationPaging() throws HttpStatusException{
        List<Location> list=new ArrayList<>();
        JSONObject result=LocationSummary();
        if(("OK").equals(result.get("ret"))){
            JSONObject summary= (JSONObject) result.get("summary");
             int total= (int) summary.get("total");
             int max= (int) summary.get("max");
             int n=total/max;
            for (int i = 0; i <=n ; i++) {
                String url=URL+"LocationPaging?cust_id="+CUSTID+"&offset="+(i*max)+"&size="+max;
                String r=HttpUtil.doGet(url,"GET");
                JSONObject obj=JSONObject.parseObject(r);
                if(("OK").equals(obj.get("ret"))){
                    List<Location> aa=JSONArray.parseArray(obj.get("paging").toString(),Location.class);
                    list.addAll(aa);
                }
            }
        }
        return list;
    }

    //车辆远程控制
    public static AjaxResult control(String carId,int io,String flag) throws HttpStatusException{
        String url=URL+"control?cust_id="+CUSTID+"&car_id="+carId+"&io="+io+"&flag="+flag;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            return AjaxResult.success("远程控制成功");
        }
        return AjaxResult.error("控制失败!");
    }

    //设备报警信息
    public static List<RetrieveAlarm> retrieveAlarm() throws HttpStatusException{
        String url=URL+"retrieveAlarm?cust_id="+CUSTID;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        List<RetrieveAlarm> alarmList=new ArrayList<>();
        if(("OK").equals(obj.get("ret"))){
            alarmList=JSONArray.parseArray(obj.get("retrieveAlarm").toString(),RetrieveAlarm.class);
        }
        return alarmList;
    }


    public static void main(String[] args) throws HttpStatusException {
        System.err.println(retrieveAlarm());
        //System.err.println(location("1"));
    }

}
